package view;

import component.button.FlatButton;
import component.edittext.InputArea;
import component.theme.ThisFont;
import component.util.MaterialBorder;
import component.util.MaterialColors;
import control.ClipBoardControl;
import entity.ClipMessage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ContentCard extends JPanel {
    private String message;
    private String date;
    private String type;
    private String device;
    public ContentCard(ClipMessage clipMessage){
        super();
        this.message = clipMessage.getContent();
        this.date = clipMessage.getDate();
        this.type = "文本";
        this.device = clipMessage.getDevice();
        init();
    }

    private void init(){
        this.setBorder(MaterialBorder.NORMAL_LINE_BORDER);
        this.setBackground(MaterialColors.WHITE);
        GridBagLayout gridBagLayout = new GridBagLayout();
        this.setLayout(gridBagLayout);
        GridBagConstraints gridBagConstraints;
        //设备信息
        JLabel deviceLabel = new JLabel("来自:"+device);
        deviceLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        deviceLabel.setOpaque(false);
        deviceLabel.setBackground(MaterialColors.WHITE);
        //日期
        gridBagConstraints = new GridBagConstraints();
        JLabel dateLabel = new JLabel(date, SwingConstants.RIGHT);
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = GridBagConstraints.EAST;
        this.add(dateLabel);
        //内容
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.anchor = GridBagConstraints.CENTER;
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        JButton copy = new JButton("复制");
        InputArea inputArea = new InputArea(null,copy,deviceLabel);
        copy.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!inputArea.getText().equals(ClipBoardControl.getClipboardString())){
                    ClipBoardControl.setClipboardString(inputArea.getText());
                }
            }
        });
        inputArea.setText(message);
        inputArea.getInputArea().setRows(5);
        inputArea.getInputArea().setBackground(MaterialColors.BLUE_50);
        inputArea.getInputArea().setEditable(false);
        //双击编辑
        inputArea.getInputArea().addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2){
                    inputArea.getInputArea().setCursor(new Cursor(Cursor.TEXT_CURSOR));
                    inputArea.getInputArea().getCaret().setVisible(true);
                    inputArea.getInputArea().setEditable(true);

                }
            }
        });
        //失去焦点不可编辑
        inputArea.getInputArea().addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                inputArea.getInputArea().setEditable(false);
            }
        });
        this.add(inputArea, gridBagConstraints);
    }
}
